package com.huaxindata.im.main.contactview;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxindata.im.R;
import com.huaxindata.im.main.manager.ContactManager;
import com.huaxindata.im.model.contact.ContactPerson;

import java.util.List;

public class ExpandableListContactAdapter extends BaseExpandableListAdapter {

    private List<String>        mGroupTitleList;
    private List<ContactPerson> mChildContactList;

    private Context mContext;
    LayoutInflater inflater;

    public ExpandableListContactAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setData(List<String> groupTitleList, List<ContactPerson> contactList) {
        this.mGroupTitleList = groupTitleList;
        this.mChildContactList = contactList;
    }

    @Override
    public int getGroupCount() {
        if (mGroupTitleList == null) {
            return 0;
        }
        return mGroupTitleList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mChildContactList == null) {
            return 0;
        }
        return mChildContactList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupTitleList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChildContactList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        Holder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_contact_group_item, parent, false);
            holder = new Holder();
            holder.icon = (AvatarImageView) view.findViewById(R.id.icon);
            holder.name = (TextView) view.findViewById(R.id.name);
            holder.line = view.findViewById(R.id.line);
            holder.top = view.findViewById(R.id.top_ex);
            holder.bottom = view.findViewById(R.id.bottom_ex);
            holder.company_ic = view.findViewById(R.id.company_ic);
            holder.expand = (ImageView) view.findViewById(R.id.expand);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.name.setText(mGroupTitleList.get(groupPosition));
        if (isExpanded) {
            holder.expand.setImageResource(R.mipmap.icon_down);
        } else {
            holder.expand.setImageResource(R.mipmap.ic_arrow_right);
        }
        holder.expand.setVisibility(View.GONE);
        holder.company_ic.setVisibility(View.GONE);
        //空白栏目
        if (groupPosition < 2) {
            if (groupPosition == 0) {
                holder.icon.setImageResource(R.mipmap.group1);
            } else {
                holder.icon.setImageResource(R.mipmap.group2);
            }
            holder.bottom.setVisibility(View.GONE);
            holder.top.setVisibility(View.GONE);
            holder.line.setVisibility(View.VISIBLE);
        } else if (groupPosition == 2) {
            holder.bottom.setVisibility(View.GONE);
            holder.top.setVisibility(View.GONE);
            holder.line.setVisibility(View.GONE);
            holder.icon.setImageResource(R.mipmap.group3);
        } else if (groupPosition == 3 && getGroupCount() == 4) {
            holder.bottom.setVisibility(View.GONE);
            holder.top.setVisibility(View.VISIBLE);
            holder.line.setVisibility(View.GONE);
            holder.icon.setImageResource(R.mipmap.group5);
            holder.expand.setVisibility(View.VISIBLE);
        } else if (groupPosition == 3 && getGroupCount() == 5) {
            holder.bottom.setVisibility(View.VISIBLE);
            holder.top.setVisibility(View.VISIBLE);
            holder.line.setVisibility(View.GONE);
            holder.icon.setImageResource(R.mipmap.group4);
            holder.company_ic.setVisibility(View.VISIBLE);
        } else if (groupPosition == 4) {
            holder.bottom.setVisibility(View.GONE);
            holder.top.setVisibility(View.GONE);
            holder.line.setVisibility(View.GONE);
            holder.icon.setImageResource(R.mipmap.group5);
            holder.expand.setVisibility(View.VISIBLE);
        }
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        Holder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_contact_child_item, parent, false);
            holder = new Holder();
            holder.icon = (AvatarImageView) view.findViewById(R.id.icon);
            holder.name = (TextView) view.findViewById(R.id.name);
            holder.duty = (TextView) view.findViewById(R.id.duty);
            holder.line = view.findViewById(R.id.line);
            holder.friend_icon = view.findViewById(R.id.friend_icon);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        ContactPerson person = mChildContactList.get(childPosition);

        if (ContactManager.isFriend(person.getClientId())) {
            holder.friend_icon.setVisibility(View.VISIBLE);
        } else {
            holder.friend_icon.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(person.getDuty())) {
            holder.duty.setVisibility(View.VISIBLE);
            holder.duty.setText(person.getDuty());
        } else {
            holder.duty.setVisibility(View.GONE);
        }
        holder.name.setText(person.getNickName());
        //Glide.with(mContext).load(person.getIcon()).into(holder.icon);
        //设置头像
        HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
        instance.setHeadImage(mContext, person.getIcon(), person.getNickName(), holder.icon);
        holder.line.setVisibility(View.VISIBLE);
        return view;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class Holder {
        AvatarImageView icon;
        ImageView expand;
        TextView name;
        View     line;
        TextView duty;
        View     friend_icon;
        View     top, bottom, company_ic;
    }
}
